<?php namespace sacvoyage\http;


require_once 'sacvoyage/http/util.php';
use \sacvoyage\http\parse_path;

class Resource {

	
	public function __construct(\sacvoyage\domain\Resource $resource) {
		$this->_resource=$resource;		
	}
	
	public function serve($path) {
		
		list($path, $rest)=parse_path($path);
		
		// logout
		if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST' && !$path 
		&& isset($_POST['action']) && $_POST['action']=='logout') {
			unset($_SESSION['user']);
			Globals::redirect('/');
		}
		
		if(!$path) {
			$path='index';
		}
		if($path=='*') {
			if(!headers_sent()) {
				header('Content-type: application/json', true);
			}
			echo save_json($this->_resource->getCollectionNames());
		} elseif($app=$this->_resource->getApplication($path)) {
			$app->serve($rest);
		} else {
			$collection=new Collection(
				$this->_resource->getCollection($path)
			);
			$collection->serve($rest);			
		} 
	}
	
	private $_resource;
	
};



?>
